package com.food.restaurantpro.database.tables;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.util.Log;

import com.food.restaurantpro.RestaurantApp;
import com.food.restaurantpro.database.RestaurantAppContentProvider;
import com.food.restaurantpro.objects.Place;

public class PlaceTable {
	public static final String TAG = "PlaceTable";

	public static final String TABLE_NAME = "place";

	public static final String PLACE_ID = "placeID";
	public static final String PLACE_NAME = "placeName";
	public static final String PLACE_DESCRIPTION = "placeDescription";
	public static final String PLACE_SHORT_INTRO = "placeShortIntro";
	public static final String PLACE_ADDRESS = "placeAddress";
	public static final String PLACE_PHONE = "placePhone";
	public static final String PLACE_OPEN = "placeOpen";
	public static final String PLACE_CLOSE = "placeClose";
	public static final String PLACE_LAT = "placeLat";
	public static final String PLACE_LONG = "placeLong";
	
	public static final String SQL_EXCUTE = "CREATE TABLE " + TABLE_NAME + "("
			+ PLACE_ID + " text primary key not null, "
			+ PLACE_NAME + " text not null, "
			+ PLACE_DESCRIPTION + " text not null, "
			+ PLACE_SHORT_INTRO + " text not null, "
			+ PLACE_ADDRESS + " text not null, "
			+ PLACE_PHONE + " text not null, "
			+ PLACE_OPEN + " text not null, "
			+ PLACE_CLOSE + " text not null, "
			+ PLACE_LAT + " text not null, "
			+ PLACE_LONG + " text not null);";
	
	public static void deleteAll()
	{
		ContentResolver resolver = RestaurantApp.CURRENT_CONTEXT.getContentResolver();
		resolver.delete(RestaurantAppContentProvider.PLACE_CONTENT_URI, null, null);
	}
	
	/**
	 * @param placeList
	 * @return the number of newly created rows. 
	 */
	public static int insertPlaceList(List<Place> placeList) {
		if(placeList != null && placeList.size() > 0) {
			int size = placeList.size();
			//show progress
			ContentValues[] allValues = new ContentValues[size];
			for (int i = 0; i < size; i++) {
				ContentValues value = new ContentValues();
				Place place = placeList.get(i);
				value.put(PLACE_ID, place.getPlaceID());
				value.put(PLACE_NAME, place.getPlaceName());
				value.put(PLACE_DESCRIPTION, place.getPlaceDescription());
				value.put(PLACE_SHORT_INTRO, place.getPlaceShortIntro());
				value.put(PLACE_ADDRESS, place.getPlaceAddress());
				value.put(PLACE_PHONE, place.getPlacePhone());
				value.put(PLACE_OPEN, place.getPlaceOpen());
				value.put(PLACE_CLOSE, place.getPlaceClose());
				value.put(PLACE_LAT, place.getPlaceLat());
				value.put(PLACE_LONG, place.getPlaceLong());
				
				allValues[i] = value;
			}
			ContentResolver resolver = RestaurantApp.CURRENT_CONTEXT.getContentResolver();
			int rowadd = resolver.bulkInsert(RestaurantAppContentProvider.PLACE_CONTENT_URI, allValues);
			return rowadd;
			//clode progress
		}
		return 0;
	}
	
	public static ArrayList<Place> getAllPlaces()
	{
		ContentResolver resolver = RestaurantApp.CURRENT_CONTEXT.getContentResolver();
		ArrayList<Place> arrayList = new ArrayList<Place>();
		Cursor cursor = resolver.query(RestaurantAppContentProvider.PLACE_CONTENT_URI, null, null, null, null);
		if(cursor != null){
			//cach 1
			/*if(cursor.moveToFirst()) {
				do{
					//TODO
				} while(cursor.moveToNext());
			}*/
			//cach 2
			if(cursor.getCount()>0){
				try {
					while (!cursor.isLast()) {
						cursor.moveToNext();
						Place place = new Place();
						
						String placeID = cursor.getString(cursor.getColumnIndex(PLACE_ID));
						place.setPlaceID(placeID);
						
						String placeName = cursor.getString(cursor.getColumnIndex(PLACE_NAME));
						place.setPlaceName(placeName);
						
						String placeDescription = cursor.getString(cursor.getColumnIndex(PLACE_DESCRIPTION));
						place.setPlaceDescription(placeDescription);
						
						String placeShortIntro = cursor.getString(cursor.getColumnIndex(PLACE_SHORT_INTRO));
						place.setPlaceShortIntro(placeShortIntro);
						
						String placeAddress = cursor.getString(cursor.getColumnIndex(PLACE_ADDRESS));
						place.setPlaceAddress(placeAddress);
						
						String placePhone = cursor.getString(cursor.getColumnIndex(PLACE_PHONE));
						place.setPlacePhone(placePhone);
						
						String placeOpen = cursor.getString(cursor.getColumnIndex(PLACE_OPEN));
						place.setPlaceOpen(placeOpen);
						
						String placeClose = cursor.getString(cursor.getColumnIndex(PLACE_CLOSE));
						place.setPlaceClose(placeClose);
						
						String placeLat = cursor.getString(cursor.getColumnIndex(PLACE_LAT));
						place.setPlaceLat(placeLat);
						
						String placeLong = cursor.getString(cursor.getColumnIndex(PLACE_LONG));
						place.setPlaceLong(placeLong);
						
						arrayList.add(place);
					}
				} catch (Exception e) {
					// TODO: handle exception
					Log.e(TAG, "ERROR getAllPlaces()", e);
				}
			}
			cursor.close();
		}
		return arrayList;
	}
}
